package com.fingerth.demo

import android.animation.Animator
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.fingerth.demo.base.BaseAct
import com.fingerth.demo.databinding.ActivityMainBinding
import com.fingerth.demo.ui.Use1Activity
import com.fingerth.demo.ui.Use2Activity
import com.fingerth.demo.ui.Use3Activity
import com.fingerth.demo.utils.ktStartActivity
import com.fingerth.demo.utils.setClick
import com.fingerth.xadapter.AnModel
import com.fingerth.xadapter.ImAnimation
import com.fingerth.xadapter.Xadapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random

class MainActivity : BaseAct<ActivityMainBinding>(), View.OnClickListener {

    override fun initView() {
        setClick(vBinding.bt1,vBinding.bt2,vBinding.bt3)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt1 -> ktStartActivity<Use1Activity>(this)
            R.id.bt2 -> ktStartActivity<Use2Activity>(this)
            R.id.bt3 -> ktStartActivity<Use3Activity>(this)
        }
    }

    private var adapter: Xadapter.XRecyclerAdapter<RvDataBean>? = null

    //    private var adapter2: Xadapter2.XRecyclerAdapter<RvDataBean>? = null
    private val data: ArrayList<RvDataBean> = ArrayList()
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
//        repeat(50) {
//            val type = Random.nextInt(3) + 1
//            data.add(RvDataBean(type, "ItemType$type"))
//        }
//        initAdapter1()
////        initAdapter2()
////        initAdapter3()
////        initAdapter4()
////        rv.layoutManager = GridLayoutManager(this, 4)
////        rv.layoutManager = StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
//
//    }

    /**
     * 简单使用1
     */
    private fun initAdapter1() {
        if (adapter == null) {
            adapter = Xadapter<RvDataBean>(this)
                    .data(data)
                    .layoutId(R.layout.item_rv_text_view)
                    .bind { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .create()
//            rv.adapter = adapter
        } else adapter!!.notify(data)

    }

    /**
     * 简单使用2
     */
    private fun initAdapter2() {
        if (adapter == null) {
            adapter = Xadapter<RvDataBean>(this)
                    .data(data)
                    .ViewTypeBuider()
                    .typeBy { it.type }
                    .typeItem(1 to R.layout.item_rv_text_view)
                    .typeItem(2 to R.layout.item_rv_text_view2)
                    .typeItem(3 to R.layout.item_rv_text_view3)
                    .typeItem(R.layout.item_rv_default_view)
                    .build()
                    .bind(1) { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .bind(2) { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .bind(3) { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .bind { _, _, _, _, _ -> }
                    .create()
//            rv.adapter = adapter
        } else adapter!!.notify(data)
    }

    /**
     * 使用动画 / 点击事件
     */
    private fun initAdapter3() {
        if (adapter == null) {
            adapter = Xadapter<RvDataBean>(this)
                    .data(data)
                    .layoutId(R.layout.item_rv_text_view)
                    .itemAnimation()
                    .bind { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .itemClickListener { _, _, _, bean, _ ->
                        Toast.makeText(this, bean.text, Toast.LENGTH_SHORT).show()
                    }
                    .create()
//            rv.adapter = adapter
        } else adapter!!.notify(data)

    }

    /**
     * 使用动画 / 自定义动画
     */
    private fun initAdapter4() {
        if (adapter == null) {
            adapter = Xadapter<RvDataBean>(this)
                    .data(data)
                    .layoutId(R.layout.item_rv_text_view)
                    .itemAnimation(object : ImAnimation {
                        override fun getAnimators(view: View?): Array<Animator> {
                            return super.getAnimators(view)
                        }

                        override fun getAnModel(): AnModel = AnModel.ONLY_DOWN
                        override fun getStart(): Int = 5
                    })
                    .bind { _, holder, _, bean, _ -> holder.setText(R.id.tv, bean.text) }
                    .create()
//            rv.adapter = adapter
        } else adapter!!.notify(data)

    }

    data class RvDataBean(val type: Int, val text: String)


}
